package com.xhwl.logistics.service.impl.http;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xhwl.common.enums.device.DeviceActiveStatusEnum;
import com.xhwl.common.enums.device.DeviceOnlineStatusEnum;
import com.xhwl.common.exception.BusinessException;
import com.xhwl.common.pojo.cent.device.Device;
import com.xhwl.logistics.bo.cent.VehicleIntegratedConfigBO;
import com.xhwl.logistics.bo.cent.VehicleParkingInfoBO;
import com.xhwl.logistics.constant.FuShiConstant;
import com.xhwl.logistics.entity.VehicleParkingLot;
import com.xhwl.logistics.entity.VehiclePassage;
import com.xhwl.logistics.util.HttpUtils;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
//@Service
public class FuShiHttpServiceImpl {

    public VehicleParkingLot getParkingLotInfo(VehicleParkingInfoBO vehicleParkingInfoBO, Integer projectId) {
        final String host = vehicleParkingInfoBO.getHost();

        Map<String, String> headerMap = new HashMap<>();
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("PageSize", 1000);
        paramMap.put("CurrentPage", 1);
        paramMap.put("OrderBy", "ID");
        paramMap.put("OrderType", false);
        paramMap.put("where", "Id>0");
        String json = JSON.toJSONString(paramMap);
        JSONObject jsonResult = HttpUtils.doPost(host + FuShiConstant.getParkingLotInfo, json, headerMap);

        Map parkingLotMap = JSONObject.toJavaObject(jsonResult, Map.class);
        Object data = parkingLotMap.get("Records");
        List<Map> maps = JSON.parseArray(data.toString(), Map.class);
        if (maps == null || maps.size() == 0)
            throw new BusinessException("未查询到停车场信息");
        if (maps.size() > 1)
            throw new BusinessException("查询到多个停车场信息");

        VehicleParkingLot vehcParkingLot = new VehicleParkingLot();
        Map dataMap = maps.get(0);
        String parkId = String.valueOf(dataMap.get("Rid"));
        String parkName = (String) dataMap.get("Name");
        Integer totalSpaceNum = (Integer) dataMap.get("LotCount");
        vehcParkingLot.setCarportTotalNumber(totalSpaceNum);
        vehcParkingLot.setParkingLotName(parkName);
        vehcParkingLot.setParkId(parkId);
        vehcParkingLot.setProjectId(projectId);
        return vehcParkingLot;
    }

    public List<VehiclePassage> getPassageList(VehicleParkingInfoBO vehicleParkingInfoBO, Integer projectId) {
        String host = vehicleParkingInfoBO.getHost();
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("PageSize", 1000);
        paramMap.put("CurrentPage", 1);
        paramMap.put("OrderBy", "ID");
        paramMap.put("OrderType", false);
        paramMap.put("where", "Id>0");
        String json = JSON.toJSONString(paramMap);
        Map<String, String> headerMap = new HashMap<>();
        JSONObject jsonResult = HttpUtils.doPost(host + FuShiConstant.getPassageInfo, json, headerMap);

        Map parkingLotMap = JSONObject.toJavaObject(jsonResult, Map.class);
        Object data = parkingLotMap.get("Records");
        List<Map> nodeListMaps = JSON.parseArray(data.toString(), Map.class);
        Integer parkId = (Integer) paramMap.get("parkId");
        List<VehiclePassage> list = new ArrayList<>();
        nodeListMaps.forEach(n -> {
            VehiclePassage vehcPassage = new VehiclePassage();
            String nodeId = String.valueOf(n.get("LaneNo"));
            String nodeName = (String) n.get("Name");
            Integer useType = (Integer) n.get("RunMode");//0-入口  1-出口  2-中央收费
            // TODO: 2023/4/11
            vehcPassage.setType(useType);
            vehcPassage.setPassageName(nodeName);
            vehcPassage.setThirdPassageId(nodeId);
            vehcPassage.setParkingLotId(parkId);
            vehcPassage.setProjectId(projectId);
            list.add(vehcPassage);
        });

        return list;
    }

    public List<Device> getParkingLotDevice(VehicleIntegratedConfigBO vehicleIntegratedConfigBO) {

        VehicleParkingInfoBO vehicleParkingInfoBO = vehicleIntegratedConfigBO.getVehicleParkingInfoBO();

        Integer projectId = vehicleIntegratedConfigBO.getVehicleSystemInfoBO().getProjectId();

        List<Device> list = new ArrayList<>();
        String host = vehicleParkingInfoBO.getHost();
        Map<String, String> headerMap = new HashMap<>();

        Map<String, Object> param = new HashMap<>();
        param.put("PageSize", 1000);
        param.put("CurrentPage", 1);
        param.put("where", "ID>0 AND (MonitorType = 21 OR MonitorType = 22 OR MonitorType = 23)");
        String jsonStatus = JSON.toJSONString(param);
        JSONObject jsonResultStatus = HttpUtils.doPost(host + FuShiConstant.getMonitorStatus, jsonStatus, headerMap);

        List<Map> mapsStatus = null;
        Map mapStatus = JSONObject.toJavaObject(jsonResultStatus, Map.class);
        if (mapStatus != null) {
            Object dataStatus = mapStatus.get("Records");
            if (dataStatus != null) {
                mapsStatus = JSON.parseArray(dataStatus.toString(), Map.class);
            }
        }

        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("PageSize", 1000);
        paramMap.put("CurrentPage", 1);
        paramMap.put("OrderBy", "ID");
        paramMap.put("OrderType", false);
        paramMap.put("where", "Id>0");
        String json = JSON.toJSONString(paramMap);

        JSONObject jsonResultReader = HttpUtils.doPost(host + FuShiConstant.getReaderDevice, json, headerMap);

        Map mapReader = JSONObject.toJavaObject(jsonResultReader, Map.class);
        if (mapReader != null) {
            Object dataReader = mapReader.get("Records");
            if (dataReader != null) {
                List<Map> mapsReader = JSON.parseArray(dataReader.toString(), Map.class);
                if (mapsReader != null) {
                    List<Map> finalMapsStatus = mapsStatus;
                    mapsReader.forEach(d -> {
                        Device device = new Device();
                        String deviceName = (String) d.get("Name");
                        String thirdDeviceId = (String) d.get("Rid");

                        if (finalMapsStatus != null) {
                            finalMapsStatus.forEach(status -> {
                                if (status.get("MonitorId").equals(d.get("Rid"))) {
                                    if ((Integer) status.get("Status") == 1) {
                                        device.setOnlineStatus(DeviceOnlineStatusEnum.ONLINE);
                                    } else {
                                        device.setOnlineStatus(DeviceOnlineStatusEnum.OFFLINE);
                                    }
                                }
                            });
                        }

                        device.setProjectId(projectId);
                        device.setThirdDeviceId(thirdDeviceId);
                        // TODO: 2023/4/11
//                        device.setDeviceTypeId()
                        device.setName(deviceName);
                        device.setDeviceStatus(DeviceActiveStatusEnum.ACTIVE.id);
                        device.setProductId(64);//对应设备device-product表
                        device.setCategoryId(3);
                        device.setDeviceTypeId(3);
                        list.add(device);
                    });
                }
            }
        }

        JSONObject jsonResultPlate = HttpUtils.doPost(host + FuShiConstant.getPlateDevice, json, headerMap);
        Map mapPlate = JSONObject.toJavaObject(jsonResultPlate, Map.class);
        if (mapPlate != null) {
            Object dataPlate = mapPlate.get("Records");
            if (dataPlate != null) {
                List<Map> mapsPlate = JSON.parseArray(dataPlate.toString(), Map.class);
                if (mapsPlate != null) {
                    List<Map> finalMapsStatus1 = mapsStatus;
                    mapsPlate.forEach(d -> {
                        Device device = new Device();
                        String deviceName = (String) d.get("Name");
                        String thirdDeviceId = (String) d.get("Rid");

                        if (finalMapsStatus1 != null) {
                            finalMapsStatus1.forEach(status -> {
                                if (status.get("MonitorId").equals(d.get("Rid"))) {
                                    if ((Integer) status.get("Status") == 1) {
                                        device.setOnlineStatus(DeviceOnlineStatusEnum.ONLINE);
                                    } else {
                                        device.setOnlineStatus(DeviceOnlineStatusEnum.OFFLINE);
                                    }
                                }
                            });
                        }
                        device.setProjectId(projectId);
                        device.setThirdDeviceId(thirdDeviceId);
                        device.setName(deviceName);
                        device.setDeviceStatus(DeviceActiveStatusEnum.ACTIVE.id);
                        device.setProductId(64);//对应设备device-product表
                        device.setCategoryId(3);
                        device.setDeviceTypeId(3);
                        list.add(device);
                    });
                }
            }
        }


        JSONObject jsonResultGate = HttpUtils.doPost(host + FuShiConstant.getGateControllerDevice, json, headerMap);
        Map mapGate = JSONObject.toJavaObject(jsonResultGate, Map.class);
        if (mapGate != null) {
            Object dataGate = mapGate.get("Records");
            if (dataGate != null) {
                List<Map> mapsGate = JSON.parseArray(dataGate.toString(), Map.class);
                if (mapsGate != null) {
                    List<Map> finalMapsStatus2 = mapsStatus;
                    mapsGate.forEach(d -> {
                        Device device = new Device();
                        String deviceName = (String) d.get("Name");
                        String thirdDeviceId = (String) d.get("Rid");

                        if (finalMapsStatus2 != null) {
                            finalMapsStatus2.forEach(status -> {
                                if (status.get("MonitorId").equals(d.get("Rid"))) {
                                    if ((Integer) status.get("Status") == 1) {
                                        device.setOnlineStatus(DeviceOnlineStatusEnum.ONLINE);
                                    } else {
                                        device.setOnlineStatus(DeviceOnlineStatusEnum.OFFLINE);
                                    }
                                }
                            });
                        }
                        device.setProjectId(projectId);
                        device.setThirdDeviceId(thirdDeviceId);
                        device.setName(deviceName);
                        device.setDeviceStatus(DeviceActiveStatusEnum.ACTIVE.id);
                        device.setProductId(64);//对应设备device-product表
                        device.setCategoryId(3);
                        device.setDeviceTypeId(3);
                        list.add(device);
                    });
                }
            }
        }

        return list;
    }

}
